def pr(a):
    kd=0
    for i in range(2,a):
        if a%i==0:
            kd+=1
            break
    if kd==0:
        return True
    else:
        return False


a1=int(input())
if a1>=1000 and a1<10000:
    if pr(a1)==True:
        print(a1)
    else:
        b=a1//1000
        e=a1%10
        w=(a1//100)%10
        p=a1%100//10
        r=a1%100
        c=a1//100
        f=(int(str(a1)[1:3]))
        d=a1//10
        n=0
        if pr(d)==True and d>n:
            n=d
        if pr(f)==True and f>n:
            n=f
        if pr(c)==True and c>n:
            n=c
        if pr(r)==True and r>n:
            n=r
        if pr(p)==True and p>n:
            n=p
        if pr(w)==True and w>n:
            n=w
        if pr(e)==True and e>n:
            n=e
        if pr(b)==True and b>n:
            n=b
        print(n) 
else:
    print(0)
        
    
